/**
 * Created by maiseries on 2018/1/12.
 */

import Request from '../utils/Request'

export default class Demand {

    static DEMAND_AUDIT_STATE = {
        APPROVING: '01',
        REJECTED: '02',
        REFUSED: '03',
        PASSED: '04',
    };

    static LOGS_STATE = {
        REQUIREMENT_COMMIT: "01", //资产管理人提交需求

        // AUTHENTICATION_COMMIT: "02", //资产管理人提交认证
        // AUTHENTICATION_REJECT: "03", //操作人认证驳回
        // AUTHENTICATION_PASS: "04", //操作人认证通过

        REQUIREMENT_REJECT: "05", //操作人驳回需求
        REQUIREMENT_REFUSE: "06", //操作人拒绝需求
        REQUIREMENT_SUPPLY: "07", //资产管理人补充附件
        REQUIREMENT_PASS: "08", //操作人通过需求并初始化资产

        // ASSET_SUPPLY: "09", //资产管理人补充资产
        // ASSET_FIRST_REJECT: "10", //操作人驳回资产
        // ASSET_FIRST_UPDATE: "11", //操作人修改资产
        // ASSET_FIRST_REFUSE: "12", //操作人拒绝资产
        // ASSET_FIRST_PASS: "13", //操作人通过资产
        //
        // ASSET_SECOND_REJECT: "14", //审核人驳回资产
        // ASSET_SECOND_UPDATE: "15", //审核人修改资产
        // ASSET_SECOND_REFUSE: "16", //审核人拒绝资产
        //
        // ASSET_SECOND_PASS: "17", //审核人通过资产并填写服务费率及上传合同附件
        // ASSET_FINISHED: "18", //操作人修改资产状态为服务完成
    };

    static async list(options) {
        options.url = '/demand/list';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async detail(options) {
        options.url = '/demand/detail';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            data.progress = data.businessLogs ? this.formatProgress(data) : [];
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static formatProgress(data) {
        const form = data.requirementDetail;
        const logs = data.businessLogs;
        const res = [
            {
                title: '提交资产需求',
                date: form.create_date,
                description: '资料提交成功',
                active: false
            }
        ];

        const firstReject = logs.reverse().find(value => value.detail_type === this.LOGS_STATE.REQUIREMENT_REJECT); //reverse 倒序后 获取 第一个驳回
        const lastSupply = logs.reverse().find(value=>value.detail_type === this.LOGS_STATE.REQUIREMENT_SUPPLY); //reverse 再次倒序后 获取 最后一个待审核

        if(!firstReject){ //判断日志不包含 资产管理人补充附件 为初审
            if(form.audit_state === this.DEMAND_AUDIT_STATE.APPROVING){ //判断审核状态是否为 正在审核
                res.push({
                    title: '初次审核需求',
                    date: '',
                    description: '',
                    type: '',
                    active: true
                })
            }else{
                res.push({
                    title: '初次审核需求',
                    date: form.audit_date,
                    description: form.audit_state !== this.DEMAND_AUDIT_STATE.PASSED? form.audit_opinion : '', //判断是否为 审核通过状态 -> 没有审核理由
                    type: 'error',
                    active: false
                })
            }
        }else{
            const totalReject = logs.filter(item=>item.detail_type === this.LOGS_STATE.REQUIREMENT_REJECT); //驳回总数
            const rejectDate = form.audit_state !== this.DEMAND_AUDIT_STATE.APPROVING ? form.audit_date : ''; //驳回时间 （判断是否出现）
            const rejectDescription = form.audit_state !== this.DEMAND_AUDIT_STATE.APPROVING && form.audit_state !== this.DEMAND_AUDIT_STATE.PASSED? form.audit_opinion : ''; //驳回理由 （待审核及审核通过时没有理由）
            //日志包含 资产管理人补充附件 时先添加初审
            res.push({
                title: '初次审核需求',
                date: firstReject.create_date, //判断只有 1 条驳回时
                description: totalReject.length === 1 ? rejectDescription : '', //判断只有 1 条驳回时
                type: 'error',
                active: false
            });
            res.push({
                title: '提交补充资料',
                date: form.audit_state === this.DEMAND_AUDIT_STATE.REJECTED ? '' : (form.audit_state === this.DEMAND_AUDIT_STATE.APPROVING ? form.update_date : (lastSupply?lastSupply.create_date:'')), // 活动时不显示时间（判断是否有补充资料 没有为空）
                description: '',
                type: '',
                active: form.audit_state === this.DEMAND_AUDIT_STATE.REJECTED
            });
            res.push({
                title: '补充资料审核',
                date: totalReject.length !== 1 ? rejectDate : '', //判断不是 1 条驳回时
                description: totalReject.length !== 1 ? rejectDescription : '', //判断不是 1 条驳回时
                type: 'error',
                active: form.audit_state === this.DEMAND_AUDIT_STATE.APPROVING
            })
        }

        if (form.audit_state === this.DEMAND_AUDIT_STATE.REFUSED) {
            res.push({
                title: '审核结果',
                date: form.audit_date,
                description: '审核已驳回',
                active: true
            });
        } else if (form.audit_state === this.DEMAND_AUDIT_STATE.PASSED) {
            res.push({
                title: '审核结果',
                date: form.audit_date,
                description: '审核已通过',
                active: true
            });
        } else {
            res.push({
                title: '审核结果',
                date: '',
                description: '',
                active: false
            });
        }

        return res;
    }

    static async upload(options) {

        options.url = '/demand/upload';
        try {
            const res = await Request.upload(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async submit(options) {

        options.url = '/demand/add';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async check() {
        let options = {};
        options.url = '/demand/allowToCommit';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async messages(options) {
        options.url = '/demand/demandChat';
        try {
            const res = await Request.token(options);
            const data = Request.valid(res);
            return {
                result: true,
                message: '成功',
                data
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }

    static async sendMessage(options) {
        options.url = '/demand/sendMessage';
        try {
            const res = await Request.token(options);
            Request.valid(res);
            return {
                result: true,
                message: '成功'
            }
        } catch (err) {
            return {
                result: false,
                message: err.message
            }
        }
    }
}